const app = getApp()
const api = require('../../utils/api')

Page({
  data: {
    selectedRange: 'week',
    startDate: '',
    endDate: '',
    today: '',
    hasGlucoseData: false,
    hasBloodPressureData: false,
    hasWeightData: false,
    glucoseStats: {
      avg: '0.0',
      std: '0.0',
      cv: '0.0'
    },
    bpStats: {
      avgSystolic: '0',
      avgDiastolic: '0',
      pulsePressure: '0'
    },
    weightStats: {
      avg: '0.0',
      change: '0.0',
      avgBMI: '0.0'
    }
  },

  onLoad() {
    // 设置今天日期
    const today = new Date();
    const formattedToday = this.formatDate(today);
    
    // 设置初始日期范围
    const sevenDaysAgo = new Date();
    sevenDaysAgo.setDate(today.getDate() - 7);
    const formattedStart = this.formatDate(sevenDaysAgo);
    
    this.setData({
      today: formattedToday,
      startDate: formattedStart,
      endDate: formattedToday
    });
    
    this.loadAllData();
  },
  
  onShow() {
    this.loadAllData();
  },
  
  // 加载所有数据
  loadAllData() {
    this.loadGlucoseData();
    this.loadBloodPressureData();
    this.loadWeightData();
  },
  
  // 日期格式化
  formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  },
  
  // 切换时间范围
  changeRange(e) {
    const range = e.currentTarget.dataset.range;
    const today = new Date();
    const formattedToday = this.formatDate(today);
    let startDate = '';
    
    switch(range) {
      case 'week':
        const sevenDaysAgo = new Date();
        sevenDaysAgo.setDate(today.getDate() - 7);
        startDate = this.formatDate(sevenDaysAgo);
        break;
      case 'month':
        const thirtyDaysAgo = new Date();
        thirtyDaysAgo.setDate(today.getDate() - 30);
        startDate = this.formatDate(thirtyDaysAgo);
        break;
      case 'season':
        const ninetyDaysAgo = new Date();
        ninetyDaysAgo.setDate(today.getDate() - 90);
        startDate = this.formatDate(ninetyDaysAgo);
        break;
      case 'custom':
        // 自定义范围时不改变当前日期
        this.setData({ selectedRange: range });
        return;
    }
    
    this.setData({
      selectedRange: range,
      startDate: startDate,
      endDate: formattedToday
    });
    
    this.loadAllData();
  },
  
  // 修改开始日期
  onStartDateChange(e) {
    this.setData({
      startDate: e.detail.value
    });
    this.loadAllData();
  },
  
  // 修改结束日期
  onEndDateChange(e) {
    this.setData({
      endDate: e.detail.value
    });
    this.loadAllData();
  },
  
  // 加载血糖数据
  loadGlucoseData() {
    // 模拟数据获取和画图
    setTimeout(() => {
      // 这里应该是从API获取数据的代码
      const hasData = Math.random() > 0.3; // 模拟有70%概率有数据
      
      if (hasData) {
        this.setData({
          hasGlucoseData: true,
          glucoseStats: {
            avg: (5 + Math.random()).toFixed(1),
            std: (0.5 + Math.random() * 0.5).toFixed(1),
            cv: (10 + Math.random() * 10).toFixed(1)
          }
        });
        
        // 这里应该有画图的代码
        // ...
      } else {
        this.setData({
          hasGlucoseData: false
        });
      }
    }, 500);
  },
  
  // 加载血压数据
  loadBloodPressureData() {
    // 模拟数据获取和画图
    setTimeout(() => {
      // 这里应该是从API获取数据的代码
      const hasData = Math.random() > 0.3; // 模拟有70%概率有数据
      
      if (hasData) {
        const systolic = Math.floor(110 + Math.random() * 20);
        const diastolic = Math.floor(70 + Math.random() * 15);
        
        this.setData({
          hasBloodPressureData: true,
          bpStats: {
            avgSystolic: systolic.toString(),
            avgDiastolic: diastolic.toString(),
            pulsePressure: (systolic - diastolic).toString()
          }
        });
        
        // 这里应该有画图的代码
        // ...
      } else {
        this.setData({
          hasBloodPressureData: false
        });
      }
    }, 700);
  },
  
  // 加载体重数据
  loadWeightData() {
    // 模拟数据获取和画图
    setTimeout(() => {
      // 这里应该是从API获取数据的代码
      const hasData = Math.random() > 0.3; // 模拟有70%概率有数据
      
      if (hasData) {
        const avg = (60 + Math.random() * 20).toFixed(1);
        const change = (Math.random() > 0.5 ? -1 : 1) * (Math.random() * 2).toFixed(1);
        const height = 1.7; // 模拟身高
        const bmi = (parseFloat(avg) / (height * height)).toFixed(1);
        
        this.setData({
          hasWeightData: true,
          weightStats: {
            avg: avg,
            change: change,
            avgBMI: bmi
          }
        });
        
        // 这里应该有画图的代码
        // ...
      } else {
        this.setData({
          hasWeightData: false
        });
      }
    }, 900);
  },
  
  // 刷新血糖数据
  refreshGlucoseData() {
    wx.showToast({
      title: '刷新中...',
      icon: 'loading',
      duration: 2000
    });
    
    this.loadGlucoseData();
  },
  
  // 刷新血压数据
  refreshBloodPressureData() {
    wx.showToast({
      title: '刷新中...',
      icon: 'loading',
      duration: 2000
    });
    
    this.loadBloodPressureData();
  },
  
  // 刷新体重数据
  refreshWeightData() {
    wx.showToast({
      title: '刷新中...',
      icon: 'loading',
      duration: 2000
    });
    
    this.loadWeightData();
  },
  
  // 前往记录页面
  goToRecord() {
    wx.navigateTo({
      url: '/pages/record/record'
    });
  }
}) 